<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

class OperationLog extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = false;

    /**
     * 保存操作日志信息
     * @author 救火队队长
     * @param array $data
     * @return int|string
     */
    public function doAdd($data = [])
    {
        $result = $this->save($data);
        if ($result) {
            return $this->getLastInsID();
        }
        return $result;
    }

    /**
     * where 搜索条件
     * @author 救火队队长
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $where = [];

        if(!empty($post['date']))
        {
            $date_string = $post['date'];
            $date_array = explode(' 到 ', $date_string);
            $sdate = strtotime($date_array[0].' 00:00:00');
            $edate = strtotime($date_array[1].' 23:59:59');
            $where[] = array('ctime', ['>=', $sdate], ['<', $edate], 'and');
        }
        if(isset($post['id']) && $post['id'] !=''){
            $where[] = ['id','in',$post['id']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = "id desc";
        return $result;
    }

    /**
     * 格式化返回数据
     * @author 救火队队长
     * @param $post
     * @return mixed
     */
    protected function tableFormat($list)
    {
        if(!$list->isEmpty()){
            foreach($list as $key=>$val){
                $list[$key]['username'] = get_manage_info($val['manage_id']);
            }
        }
        return parent::tableFormat($list);
    }

}